﻿#region Using Statements
using System;
using System.Linq;
using System.Windows.Forms;
using DirectZZT.Shared.Core;
#endregion

namespace DirectZZT.Forms
{
    /// <summary>
    /// Provides a window that displays logged information.
    /// </summary>
    public partial class LoggerWindow : Form
    {
        #region Static fields

        /// <summary>
        /// Whether or not this window is opened.
        /// </summary>
        internal static bool IsOpen { get; private set; }

        #endregion

        #region Fields

        private Timer _timer;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the LoggerWindow class.
        /// </summary>
        public LoggerWindow()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Called when the dialog is shown.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            IsOpen = true;

            ListLoggers();
        }

        /// <summary>
        /// Called when the dialog is closed.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            StopTimer();
            IsOpen = false;
        }

        private void ListLoggers()
        {
            lsbLoggers.BeginUpdate();
            lsbLoggers.Items.Clear();

            foreach (var logger in Logging.GetRegisteredLoggings())
            {
                lsbLoggers.Items.Add(logger);
            }

            lsbLoggers.EndUpdate();
        }

        private void ListCurrent()
        {
            lsvMessages.BeginUpdate();
            lsvMessages.Items.Clear();

            string logger = lsbLoggers.Text;
            if (logger != null)
            {
                foreach (var entry in Logging.GetLogging(logger).GetEntries(Program.Start).Take(100))
                {
                    ListViewItem lvi = new ListViewItem(entry.Message);
                    lvi.SubItems.Add(entry.Timestamp.ToString());
                    lvi.SubItems.Add(entry.Source);
                    lvi.SubItems.Add(entry.Category);
                    lsvMessages.Items.Add(lvi);
                }
            }

            lblMessages.Text = string.Format("Messages ({0}):", lsvMessages.Items.Count);
            lsvMessages.EndUpdate();
        }

        private void StopTimer()
        {
            if (_timer != null)
            {
                _timer.Stop();
                _timer.Tick -= RefreshTimer_Tick;
                _timer.Dispose();
                _timer = null;
            }
        }

        #endregion

        #region Handled events

        private void lsbLoggers_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListCurrent();
        }

        private void chkRefreshLog_CheckedChanged(object sender, EventArgs e)
        {
            this.numRefreshPeriod.Enabled = this.chkRefreshLog.Checked;

            if (this.chkRefreshLog.Checked)
            {
                _timer = new Timer();
                _timer.Interval = (int)numRefreshPeriod.Value;
                _timer.Tick += RefreshTimer_Tick;
                _timer.Start();
            }
            else
            {
                StopTimer();
            }
        }

        private void RefreshTimer_Tick(object sender, EventArgs e)
        {
            ListCurrent();
        }

        private void numRefreshPeriod_ValueChanged(object sender, EventArgs e)
        {
            if (_timer != null)
            {
                _timer.Interval = (int)numRefreshPeriod.Value;
            }
        }

        #endregion
    }
}
